package com.chuji.crmcj.act;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.utils.DataCleanUtil;
import com.chuji.crmcj.utils.Jump;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.view.CalendarViewDialog;

public class SetActivity extends Activity {
    /*关于我们*/
    private RelativeLayout rl_aboutUs;
    /*反馈与意见*/
    private RelativeLayout rl_feedback;
    /*显示缓存*/
    private TextView tv_cache;
    /*清除缓存*/
    private RelativeLayout rl_clearcache;
    /*清除缓存效果显示*/
    private ProgressDialog pd;
    /*退出登录*/
    private Button btn_cancel;
    /*返回*/
    private ImageView iv_back;
    WindowManager windowManager;
    CalendarViewDialog dialog;
    WindowManager.LayoutParams lp;

    @Override
    protected void onStart() {
        super.onStart();
        try {
            tv_cache.setText(DataCleanUtil.getTotalCacheSize(UIUtils.getContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        /**
         * 初始化组件
         */
        rl_aboutUs = (RelativeLayout) findViewById(R.id.rl_aboutUs);
        rl_feedback = (RelativeLayout) findViewById(R.id.rl_feedback);
        tv_cache = (TextView) findViewById(R.id.tv_cache);
        rl_clearcache = (RelativeLayout) findViewById(R.id.rl_clearcache);
        btn_cancel = (Button) findViewById(R.id.btn_cancel);
        iv_back = (ImageView) findViewById(R.id.iv_Back);
        //初始化progressDialog
        pd = new ProgressDialog(this);
        pd.setMessage("正在清除缓存");
        /**
         * 跳转到关于我们
         */
        rl_aboutUs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Jump.toNextActivity(SetActivity.this, AboutUsActivity.class);
            }
        });
        /**
         * 跳转到反馈与建议
         */
        rl_feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Jump.toNextActivity(SetActivity.this, FeedBackActivity.class);
            }
        });
        /**
         * 清除缓存
         */
        rl_clearcache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCleanDialog();
            }
        });
        /**
         * 退出登录
         */
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCancelDialog();
            }
        });
        /**
         * 返回
         */
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void showCleanDataDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_clean_data, null);// 给dialog设定特定布局
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
        windowManager = getWindowManager();
        lp = dialog.getWindow().getAttributes();  //获取对话框当前的参数值
        //p.height = (int) (d.getHeight() * 0.3);   //高度设置为屏幕的0.3
        lp.width = windowManager.getDefaultDisplay().getWidth();//为获取屏幕宽、高,宽度设置为全屏
        dialog.getWindow().setAttributes(lp);     //设置生效
        dialog.show();
        DataCleanUtil.clearAllCache(UIUtils.getContext());
        try {
            if (DataCleanUtil.getTotalCacheSize(UIUtils.getContext()) == "0K") {
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);//让他显示10秒后，取消ProgressDialog

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        dialog.dismiss();
                        UIUtils.showToastSafe("清除缓存成功");
                        // 使用postInvalidate可以直接在线程中更新界面
                    }
                });
                t.start();
                tv_cache.setText("0K");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 清除缓存
     */
    private void showCleanDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_clean_data_choose, null);// 给dialog设定特定布局
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);
        final TextView mTvFolType = (TextView) view.findViewById(R.id.tv_isfollow);
        mBtSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCleanDataDialog();
                dialog.dismiss();
            }
        });
        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    /**
     * 注销登录
     */
    private void showCancelDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();
        View view = View.inflate(this, R.layout.dialog_cancel_login, null);// 给dialog设定特定布局
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);// 去掉上下左右边距, 兼容2.x版本
        Button mBtSave = (Button) view.findViewById(R.id.btn_sure);
        Button mBtCancel = (Button) view.findViewById(R.id.btn_cancel);
        final TextView mTvFolType = (TextView) view.findViewById(R.id.tv_isfollow);
        mBtSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                SPUtils.saveString(UIUtils.getContext(),"","");
//                SPUtils.saveString(UIUtils.getContext(),"","");
                SPUtils.saveBoolean(UIUtils.getContext(), "zhuxiao", true);
                UIUtils.startActivity(new Intent(UIUtils.getContext(), LoginActivity.class));
                SPUtils.saveInt(UIUtils.getContext(), "RoleType", -1);

                MainActivity.instance.finish();
                finish();
                dialog.dismiss();
            }
        });
        mBtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}
